package com.example.saveword.save_word;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.saveword.R;
import com.example.saveword.dao.WordDao;
import com.example.saveword.model.Word;
import com.example.saveword.utils.AudioMediaPlayer;
import com.example.saveword.utils.Sentence_split;

public class WordDetailActivity extends AppCompatActivity implements View.OnClickListener{
    WordDao wordDao;
    // 单词详情
    private int wordId;
    private Word word;
    // 要显示单词内容的控件
    private TextView tv_word_title, tv_uk_phone, tv_us_phone, tv_tranCN, tv_tranEN, tv_sentences, tv_phrases, tv_synonyms;
    // 播放音频
    private ImageView iv_play_voice_uk, iv_play_voice_us;
    private AudioMediaPlayer audioMediaPlayer; // 播放音频类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_detail);
        init();
    }

    private void getData() {
        wordDao = new WordDao(this);
        Bundle bundle = getIntent().getExtras();
        wordId = bundle.getInt("wordId");
        word = wordDao.find(wordId);
    }

    private void init() {
        getData();
        tv_word_title = findViewById(R.id.tv_word_title);
        tv_uk_phone = findViewById(R.id.tv_uk_phone);
        tv_us_phone = findViewById(R.id.tv_us_phone);
        tv_tranCN = findViewById(R.id.tv_tranCN);
        tv_tranEN = findViewById(R.id.tv_tranEN);
        tv_sentences = findViewById(R.id.tv_sentences);
        tv_phrases = findViewById(R.id.tv_phrases);
        tv_synonyms = findViewById(R.id.tv_synonyms);
        iv_play_voice_uk = findViewById(R.id.iv_play_voice_uk);
        iv_play_voice_us = findViewById(R.id.iv_play_voice_us);

        tv_word_title.setText(word.getHeadWord());
        tv_uk_phone.setText(word.getUkphone());
        tv_us_phone.setText(word.getUsphone());
        tv_tranCN.setText(word.getTranCN());
        tv_tranEN.setText(word.getTranEN());
        tv_sentences.setText(word.getSentences());
        tv_phrases.setText(word.getPhrases());
        tv_synonyms.setText(word.getSyno());

        audioMediaPlayer = new AudioMediaPlayer(this);

        iv_play_voice_uk.setOnClickListener(this);
        iv_play_voice_us.setOnClickListener(this);
        tv_sentences.setOnClickListener(this);
        tv_tranEN.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //播放单词语音
        switch (view.getId()) {
            case R.id.iv_play_voice_uk://播放单词声音
                audioMediaPlayer.updateMP(word.getHeadWord(), 0);
                break;
            case R.id.iv_play_voice_us:
                audioMediaPlayer.updateMP(word.getHeadWord(), 1);
                break;
            case R.id.tv_sentences:
                audioMediaPlayer.updateMP(Sentence_split.getspit(word.getSentences()), 0);
                break;
            case R.id.tv_tranEN:
                audioMediaPlayer.updateMP(word.getTranEN(), 0);
                break;
        }
    }
}